﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.NetworkObjectRef
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Runtime.CompilerServices;

#nullable disable
namespace Netick.Unity;

/// <summary>
/// Represents a network-serializable reference to a <see cref="T:Netick.Unity.NetworkObject" /> within the networked simulation.
/// This struct stores the object network id, allowing retrieval of the referenced object from a <see cref="T:Netick.Unity.NetworkSandbox" />.
/// </summary>
/// <typeparam name="T">The type of <see cref="T:Netick.Unity.NetworkBehaviour" /> being referenced.</typeparam>
[Serializable]
public struct NetworkObjectRef : IEquatable<NetworkObjectRef>
{
  public readonly int ObjectId;

  public NetworkObjectRef(NetworkObject obj)
  {
    this.ObjectId = (UnityEngine.Object) obj != (UnityEngine.Object) null ? obj.Id : -1;
  }

  public bool IsValid => this.ObjectId > 0;

  public bool TryGetObject(NetworkSandbox sandbox, out NetworkObject obj)
  {
    NetworkObject networkObject;
    if (sandbox.TryGetObject(this.ObjectId, out networkObject))
    {
      obj = networkObject;
      return true;
    }
    obj = (NetworkObject) null;
    return false;
  }

  public NetworkObject GetObject(NetworkSandbox sandbox)
  {
    NetworkObject networkObject;
    return sandbox.TryGetObject(this.ObjectId, out networkObject) ? networkObject : (NetworkObject) null;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public override bool Equals(object other)
  {
    return other is NetworkObjectRef other1 && this.Equals(other1);
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public override int GetHashCode() => this.ObjectId.GetHashCode();

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public bool Equals(NetworkObjectRef other) => this.ObjectId.Equals(other.ObjectId);

  public static bool operator ==(NetworkObjectRef a, NetworkObjectRef b) => a.Equals(b);

  public static bool operator !=(NetworkObjectRef a, NetworkObjectRef b) => !a.Equals(b);

  public override string ToString() => $"Object Id: {this.ObjectId}";
}
